/*
 * MacWindowsManager.java
 *
 * Created on October 25, 2005, 12:09 PM
 *
 */

package ika.gui;

import java.awt.*;
import com.muchsoft.util.mac.*;

/**
 * Mac OS X specific code. Integrates a Java application to the standard Mac 
 * look and feel.
 * @author Bernhard Jenny, Institute of Cartography, ETH Zurich.
 */
public class MacWindowsManager extends MainWindow implements Java14Handler {

    public static Frame emptyWindow;

    static void updateVisibilityOfEmptyWindow() {
        if (com.muchsoft.util.Sys.isMacOSX() && emptyWindow != null) {
            emptyWindow.setVisible(MainWindow.windows.isEmpty());
        }
    }

    /**
     * Creates a new instance of MacWindowsManager 
     */
    public MacWindowsManager() {
        super("<invisible manager window>", false);

        // hide the window: make transparent, undecorate, move out of screen area.
        this.setBackground(new Color(1.0F, 1.0F, 1.0F, 0.0F));
        this.getContentPane().removeAll();
        this.removeNotify();    // call this otherwise setUndecorated will fail.
        this.setUndecorated(true);
        this.setFocusable(false);
        this.setLocation(0, 0);
        this.setSize(new Dimension(0, 0));
        this.setResizable(false);
        this.getRootPane().putClientProperty("Window.style", "small");
        this.getRootPane().putClientProperty("Window.alpha", new Float(0.0f));
        this.setAlwaysOnTop(true);
        this.pack();
        
        if (com.muchsoft.util.Sys.isMacOSX()) {
            //this.setVisible(true);
            
            // register this object so that apple menu commands are handled.
            Java14Adapter.registerJava14Handler(this);
            
            // preferences item in the menu
            Java14Adapter.setEnabledPrefs(true);
        }
    }
    
    /* A series of Mac specific handlers that are part of the Java14Handler interface. */
    
    public void handleAbout(java.util.EventObject event) {
        ika.gui.ProgramInfo.showInfo();
        Java14Adapter.setHandled(event, true);
    }
    
    public void handleOpenApplication(java.util.EventObject event) {
    }
    
    public void handleReOpenApplication(java.util.EventObject event) {
    }
    
    public void handleOpenFile(java.util.EventObject event, String newFilePath) {
        openProject(newFilePath);
    }
    
    public void handlePrefs(java.util.EventObject event) {
       PreferencesDialog.showPreferencesDialog();
    }
    
    public void handlePrintFile(java.util.EventObject event, String filename) {
    }
    
    public void handleQuit(java.util.EventObject event) {
        // close all windows
        int nbrWindows = MainWindow.windows.size();
        boolean windowsClosed = nbrWindows < 1;
        for (int i = nbrWindows - 1; i >= 0; i--) {
            MainWindow w = (MainWindow)MainWindow.windows.get(i);
            windowsClosed = w.closeProject();
            if (!windowsClosed) // stop if user cancels closing of windows
                break;
        }  
        Java14Adapter.setHandled(event, windowsClosed);
    }
}
